package com.example.demo.exception;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.Map;

@RestController
public class MyErrorController implements ErrorController {
    private static final String ERROR_PATH = "/error";

    @RequestMapping(value = ERROR_PATH)
    public String handleError(ServerRequest request) {
//        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        Map<String, Object> attributes = request.attributes();
        String retMsg = "";
//        if (statusCode == 404) {
//            System.out.println("==================404");
//            retMsg = "==================404";
//        } else if (statusCode == 401) {
//            System.out.println("==================401");
//            retMsg = "==================401";
//        } else if (statusCode == 403) {
//            System.out.println("==================403");
//            retMsg = "==================403";
//        } else {
//            System.out.println("==================other error");
//            retMsg = "==================other error";
//        }
        return retMsg;
    }

    @Override
    public String getErrorPath() {
        System.out.println("------------enter getErrorPath");
        return ERROR_PATH;
    }

    @GetMapping("normal")
    public String test() {
        System.out.println("------------enter test");
        return "error";
    }
}